#include "infolurudlg.h"
#include "ui_infolurudlg.h"
#include <QMessageBox>

QStringList infolurudlg::poList;
QStringList infolurudlg::styleList;
QStringList infolurudlg::sizeList;
QStringList infolurudlg::ponumList;

infolurudlg::infolurudlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::infolurudlg)
{
    ui->setupUi(this);

    setWindowTitle(tr("信息录入"));
    resize(800, 600);
    //    this->showFullScreen();

    mKeyBoard = new KeyBoardView(this);
    int h = 70 * 4 + 40;
    mKeyBoard->setGeometry(0, 600-h, 800, h);
    mKeyBoard->setUI();
    mKeyBoard->setVisible(false);

    ui->listpo->addItems(poList);
    ui->liststyle->addItems(styleList);
    ui->listsize->addItems(sizeList);

    ui->btPO->setText(Start::getInstance().strPo);
    ui->btStyle->setText(Start::getInstance().strStyle);
    ui->btSize->setText(Start::getInstance().strSize);
    ui->btPOnum->setText(Start::getInstance().strPonum);

    ui->btShowkey->setVisible(false);
}

infolurudlg::~infolurudlg()
{
    delete ui;
}

void infolurudlg::inputKey(QString key)
{
    if (key == "Blank") {
        key = " ";
    }
    if (key == "Enter") {
        mKeyBoard->setVisible(false);
    } else if (key == "Backspace") {
        if (editIndex == 0) {
            QString strPo = ui->btPO->text();
            if (strPo.length() > 0) {
                strPo = strPo.left(strPo.length()-1);
                ui->btPO->setText(strPo);
            }
        } else if (editIndex == 1) {
            QString strStyle = ui->btStyle->text();
            if (strStyle.length() > 0) {
                strStyle = strStyle.left(strStyle.length()-1);
                ui->btStyle->setText(strStyle);
            }
        } else if (editIndex == 2) {
            QString strSize = ui->btSize->text();
            if (strSize.length() > 0) {
                strSize = strSize.left(strSize.length()-1);
                ui->btSize->setText(strSize);
            }
        } else {
            QString strPOnum = ui->btPOnum->text();
            if (strPOnum.length() > 0) {
                strPOnum = strPOnum.left(strPOnum.length()-1);
                ui->btPOnum->setText(strPOnum);
            }
        }
    } else if (key == "Clear") {

        if (editIndex == 0) {
            ui->btPO->setText("");
        } else if (editIndex == 1) {
            ui->btStyle->setText("");
        } else if (editIndex == 2) {
            ui->btSize->setText("");
        } else {
            ui->btPOnum->setText("");
        }
    }  else if (key == "CapsLk") {
        mKeyBoard->onCapsLK();
    } else {
        if (editIndex == 0) {
            QString strPo = ui->btPO->text();
            strPo = strPo + key;
            ui->btPO->setText(strPo);
        } else if (editIndex == 1) {
            QString strStyle = ui->btStyle->text();
            strStyle = strStyle + key;
            ui->btStyle->setText(strStyle);
        } else if (editIndex == 2) {
            if ((key >= "0" && key <= "9") || key == ".") {
                QString strSize = ui->btSize->text();
                strSize = strSize + key;
                ui->btSize->setText(strSize);
            }
        } else {
            if (key >= "0" && key <= "9") {
                QString strPOnum = ui->btPOnum->text();
                strPOnum = strPOnum + key;
                ui->btPOnum->setText(strPOnum);
            }
        }
    }
}

void infolurudlg::on_btShowkey_clicked()
{
    mKeyBoard->setVisible(true);
}

void infolurudlg::on_btSave_clicked()
{
    QString strPo = ui->btPO->text();
    QString strStyle = ui->btStyle->text();
    QString strSize = ui->btSize->text();

    if (strPo.isEmpty()) {
        QMessageBox::warning(nullptr, "", tr("请输入订单"));
        return;
    }

    Start::getInstance().strPo = strPo;
    Start::getInstance().strStyle = strStyle;
    Start::getInstance().strSize = strSize;
    Start::getInstance().strPonum =  ui->btPOnum->text();

    if (!poList.contains(strPo)) {
        poList << strPo;
    }
    if (!styleList.contains(strStyle)) {
        styleList << strStyle;
    }
    if (!sizeList.contains(strSize)) {
        sizeList << strSize;
    }

//    ponumList << Start::getInstance().strPonum;

    done(1);
}

void infolurudlg::on_btPO_clicked()
{
    editIndex = 0;
    mKeyBoard->setVisible(true);
}

void infolurudlg::on_btStyle_clicked()
{
    editIndex = 1;
    mKeyBoard->setVisible(true);
}

void infolurudlg::on_btSize_clicked()
{
    editIndex = 2;
    mKeyBoard->setVisible(true);
}

void infolurudlg::on_btPOnum_clicked()
{
    editIndex = 3;
    mKeyBoard->setVisible(true);

}

void infolurudlg::on_listpo_itemClicked(QListWidgetItem *item)
{
    ui->btPO->setText(item->text());
}

void infolurudlg::on_liststyle_itemClicked(QListWidgetItem *item)
{
    ui->btStyle->setText(item->text());
}

void infolurudlg::on_listsize_itemClicked(QListWidgetItem *item)
{
    ui->btSize->setText(item->text());
}

